package com.sk.controller;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSONObject;
import com.sk.common.CommonResponse;
import com.sk.common.DynamicDataSourceHolder;
import com.sk.config.DynamicDataSourceConfig;
import com.sk.constant.DynamicSourceConstant;
import com.sk.entity.DataSourceInfo;
import com.sk.entity.Server;
import com.sk.entity.SysNetty;
import com.sk.service.IServerService;
import com.sk.service.ISysNettyService;
import com.sk.util.DataSourceUtil;
import com.sk.util.SqlFileUtil;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.api.scripting.ScriptUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

/**
 *测试
 * @author jiaok
 * @since 2023-12-12
 */
@Slf4j
@RestController
@RequestMapping("/sys-netty")
public class SysNettyController {

    @Resource
    private DataSourceUtil dataSourceUtil;
    @Autowired
    private ISysNettyService iSysNettyService;

    @Autowired
    private IServerService iServerService;

    @Resource
    private DynamicDataSourceConfig dynamicDataSourceConfig;

    @ApiOperation("数据库操作测试")
    @PostMapping({"sqlTest"})
    public CommonResponse sqlTest() {
        DruidDataSource dataSource = (DruidDataSource) dynamicDataSourceConfig.getDefineTargetDataSources()
                .get(DynamicDataSourceHolder.getDynamicDataSourceKey());

        try {
            DruidPooledConnection connection = dataSource.getConnection();
            //创建数据库
            SqlFileUtil.createDatabase(connection,"baseTest");

            log.info("===========================  准备切换数据源  ===========================");
            DataSourceInfo dataSourceInfo = new DataSourceInfo();
            dataSourceInfo.setUrl("jdbc:mysql://127.0.0.1:3306/baseTest?characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true");
            dataSourceInfo.setUserName("root");
            dataSourceInfo.setPassword("root");

            log.info("===========================  开始建立连接  ===========================");
            DruidDataSource dataSourceConnection = dataSourceUtil.createDataSourceConnection(dataSourceInfo);
            if (Objects.nonNull(dataSourceConnection)) {
                //将新的数据源连接追加到目标数据源中
                dataSourceUtil.addDynamicDataSource("baseTest", dataSourceConnection);
                //设置当前线程数据源名称
                DynamicDataSourceHolder.setDynamicDataSourceKey("baseTest");
                DruidDataSource newSource = (DruidDataSource) dynamicDataSourceConfig.getDefineTargetDataSources()
                        .get(DynamicDataSourceHolder.getDynamicDataSourceKey());
                SqlFileUtil.executeSqlFile(newSource.getConnection(),"src/main/resources/sql/sk_frame_demo.sql");

            }

        } catch (SQLException | IOException e) {
            throw new RuntimeException(e);
        }

        return CommonResponse.ok("sql操作成功");
    }

    @ApiOperation("多数据源测试")
    @PostMapping({"mutiSource"})
    public CommonResponse cameraFunctionList() {

        log.info("===========================  当前使用的数据源：{}  ===========================", DynamicDataSourceHolder.getDynamicDataSourceKey());
        DynamicDataSourceHolder.setDynamicDataSourceKey(DynamicSourceConstant.BASEDB);
        List<Server> list = iServerService.list();
        for (Server server : list) {
            log.info("===========================  {} ===========================", JSONObject.toJSONString(server));
        }

        log.info("===========================  准备切换数据源  ===========================");
        DataSourceInfo dataSourceInfo = new DataSourceInfo();
        dataSourceInfo.setUrl("jdbc:mysql://127.0.0.1:3306/thick_steel?characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true");
        dataSourceInfo.setUserName("root");
        dataSourceInfo.setPassword("root");

        log.info("===========================  开始建立连接  ===========================");
        DruidDataSource dataSourceConnection = dataSourceUtil.createDataSourceConnection(dataSourceInfo);
        if (Objects.nonNull(dataSourceConnection)){
            //将新的数据源连接追加到目标数据源中
            dataSourceUtil.addDynamicDataSource("thick_steel",dataSourceConnection);
            //设置当前线程数据源名称
            DynamicDataSourceHolder.setDynamicDataSourceKey("thick_steel");
            Server server = iServerService.getById(21);
            log.info("===========================  当前使用的数据源：{}  ===========================", DynamicDataSourceHolder.getDynamicDataSourceKey());
            log.info("===========================  {}  ===========================",JSONObject.toJSONString(server));
        }
        return CommonResponse.ok("测试结束");
    }


}
